package com.boot2.core.conf;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.MyDateCodec;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.boot2.core.constant.I18nMessageContantKey;
import com.boot2.core.utils.DateUtil;
import com.boot2.core.utils.MessageSourceUtil;
import com.boot2.core.web.DecipherLongCodec;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangweilin
 * @date 2018/7/4 16:57
 * @description: fastjson配置，包括日期格式化，包括json一些特性规则，包括id的加解密的配置等操作
 */
@Configuration
public class FastJsonConfigs {

    /**
     * 单独定义String类型的转换器,防止被多次转义
     *
     * @return
     */
//    @ConditionalOnMissingBean(name="stringHttpMessageConverter")
//    @Bean(value = "stringHttpMessageConverter2")
//    public HttpMessageConverter<String> stringHttpMessageConverter() {
//        StringHttpMessageConverter converter = new StringHttpMessageConverter(
//                Charset.forName("UTF-8"));
//        return converter;
//    }
    @Bean(value = "fastJsonHttpMessageConverter")
    public HttpMessageConverter fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect);
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
        String dateFormat = MessageSourceUtil.getI18nMessage(I18nMessageContantKey.date_format_out, DateUtil.PATTERN_yyyy_MM_dd);
        String datetimeFormat = MessageSourceUtil.getI18nMessage(I18nMessageContantKey.datetime_format_out, DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
        String timeFormat = MessageSourceUtil.getI18nMessage(I18nMessageContantKey.time_format_out, DateUtil.PATTERN_HH_mm_ss);
        fastJsonConfig.setDateFormat(dateFormat);

//        dateFormat = "dd-MM-yyyy HH:mm:ss";

        //序列化，从后端到页面
        SerializeConfig serializeConfig = fastJsonConfig.getSerializeConfig();
        MyDateCodec myDateCodec = new MyDateCodec(dateFormat, datetimeFormat, timeFormat);
        serializeConfig.put(Date.class, myDateCodec);
        serializeConfig.put(Timestamp.class, myDateCodec);

        //此处为序列化，即java对象到json,而由接收页面的字符串到对象，在 BaseController的initBingder中
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);

        fastJsonConfig.setSerializeConfig(serializeConfig);

        //反序列化，则从页面到后台
        ParserConfig parserConfig = fastJsonConfig.getParserConfig();
        // id解密处理
        parserConfig.putDeserializer(Long.class, new DecipherLongCodec());
        parserConfig.putDeserializer(long.class, new DecipherLongCodec());
        fastJsonConfig.setParserConfig(parserConfig);

        fastConverter.setFastJsonConfig(fastJsonConfig);
        //处理中文乱码问题
        List<MediaType> oFastMediaTypeList = new ArrayList<>();
        oFastMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(oFastMediaTypeList);
        return fastConverter;
    }
}
